﻿//using Microsoft.EntityFrameworkCore;
//using Microsoft.Extensions.DependencyInjection;
//using Microsoft.Extensions.Logging;
//using System.Diagnostics;

//namespace ELF.Infrastructure.Data;

//public static class InitialiserExtensions
//{
//    public static async Task InitialiseDatabaseAsync(this IServiceProvider serviceProvider)
//    {
//        using var scope = serviceProvider.CreateScope();

//        var initialiser = scope.ServiceProvider.GetRequiredService<ApplicationDbContextInitialiser>();

//        await initialiser.InitialiseAsync();

//        await initialiser.SeedDataAsync();
//    }
//}

//public class ApplicationDbContextInitialiser
//{
//    private readonly ILogger<ApplicationDbContextInitialiser> _logger;
//    private readonly IServiceProvider serviceProvider;
//    public const string ActivitySourceName = "Migrations";
//    private static readonly ActivitySource s_activitySource = new(ActivitySourceName);

//    public ApplicationDbContextInitialiser(ILogger<ApplicationDbContextInitialiser> logger, IServiceProvider serviceProvider)
//    {
//        _logger = logger;
//        this.serviceProvider = serviceProvider;
//    }

//    public async Task InitialiseAsync()
//    {
//        try
//        {
//            using var scope = serviceProvider.CreateScope();
//            var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
//            var strategy = db.Database.CreateExecutionStrategy();
//            await strategy.ExecuteAsync(async () =>
//            {
//                // Run migration in a transaction to avoid partial migration if it fails.
//                await db.Database.MigrateAsync();
//            });
//        }
//        catch (Exception ex)
//        {
//            _logger.LogError(ex, "An error occurred while initialising the database.");
//            throw;
//        }
//    }

//    public async Task SeedDataAsync()
//    {
//        try
//        {
//            //using var scope = serviceProvider.CreateScope();
//            //var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
//            //await db.Database.EnsureCreatedAsync();
//            await Task.CompletedTask;
//        }
//        catch (Exception ex)
//        {
//            _logger.LogError(ex, "An error occurred while initialising the database.");
//            throw;
//        }
//    }
//}
